#jinja2: trim_blocks:True,lstrip_blocks:True
---

scope: {{ patroni_cluster_name }}
name: {{ ansible_hostname }}
namespace: /service/

{% if patroni_log_destination == 'logfile' %}
log:
  level: {{ patroni_log_level |upper }}
  traceback_level: {{ patroni_log_traceback_level |upper }}
  format: {{ patroni_log_format |quote }}
  dateformat: {{ patroni_log_dateformat |quote }}
  max_queue_size: {{ patroni_log_max_queue_size |int }}
  dir: {{ patroni_log_dir }}
  file_num: {{ patroni_log_file_num |int }}
  file_size: {{ patroni_log_file_size |int }}
  loggers:
    patroni.postmaster: {{ patroni_log_loggers_patroni_postmaster |upper }}
    urllib3: {{ patroni_log_loggers_urllib3 |upper }}
{% endif %}

restapi:
  listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008
  connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008
#  certfile: /etc/ssl/certs/ssl-cert-snakeoil.pem
#  keyfile: /etc/ssl/private/ssl-cert-snakeoil.key
#  authentication:
#    username: username
#    password: password

{% if not dcs_exists|bool and dcs_type == 'etcd' %}
etcd:
  hosts: {% for host in groups['etcd_cluster'] %}{{ hostvars[host]['inventory_hostname'] }}:2379{% if not loop.last %},{% endif %}{% endfor %}
{% endif %}
{% if dcs_exists|bool and dcs_type == 'etcd' %}
etcd:
  hosts: {% for etcd_hosts in patroni_etcd_hosts %}{{etcd_hosts.host}}:{{etcd_hosts.port}}{% if not loop.last %},{% endif %}{% endfor %}
{% endif %}


bootstrap:
  method: {{ patroni_cluster_bootstrap_method }}
{% if patroni_cluster_bootstrap_method == 'wal-g' %}
  wal-g:
    command: {{ wal_g_patroni_cluster_bootstrap_command }}
    no_params: True
    recovery_conf:
       recovery_target_action: promote
       recovery_target_timeline: latest
       restore_command: wal-g wal-fetch %f %p
{% endif %}
{% if patroni_cluster_bootstrap_method == 'pg_probackup' %}
  pg_probackup:
    command: {{ pg_probackup_patroni_cluster_bootstrap_command }}
    no_params: true
{% endif %}
  dcs:
    ttl: {{ patroni_ttl |d(30, true) |int }}
    loop_wait: {{ patroni_loop_wait |d(10, true) |int }}
    retry_timeout: {{ patroni_retry_timeout |d(10, true) |int }}
    maximum_lag_on_failover: {{ patroni_maximum_lag_on_failover |d(1048576, true) |int }}
    master_start_timeout: {{ patroni_master_start_timeout |d(300, true) |int }}
    synchronous_mode: {{ synchronous_mode |string |d(false, true) |lower }}
    synchronous_mode_strict: {{ synchronous_mode_strict |string |d(false, true) |lower }}
    synchronous_node_count: {{ synchronous_node_count |d(1, true) |int }}
  {% if patroni_standby_cluster.host is defined and patroni_standby_cluster.host | length > 0 %}
    standby_cluster:
      host: {{ patroni_standby_cluster.host }}
      port: {{ patroni_standby_cluster.port }}
    {% if patroni_standby_cluster.primary_slot_name is defined and patroni_standby_cluster.primary_slot_name | length > 0 %}
      primary_slot_name: {{ patroni_standby_cluster.primary_slot_name }}
    {% endif %}
    {% if patroni_standby_cluster.restore_command is defined and patroni_standby_cluster.restore_command | length > 0 %}
      restore_command: {{ patroni_standby_cluster.restore_command }}
    {% endif %}
    {% if patroni_standby_cluster.recovery_min_apply_delay is defined and patroni_standby_cluster.recovery_min_apply_delay | length > 0 %}
      recovery_min_apply_delay: {{ patroni_standby_cluster.recovery_min_apply_delay }}
    {% endif %}
  {% endif %}
    postgresql:
      use_pg_rewind: {{ patroni_postgresql_use_pg_rewind |string |d(false, true) |lower }}
      use_slots: true
      parameters:
      {% for parameter in postgresql_parameters %}
        {{ parameter.option }}: {{ parameter.value }}
      {% endfor %}

{% if postgresql_exists == 'true' %}
#  initdb:  # List options to be passed on to initdb
#    - encoding: UTF8
#    - data-checksums
{% endif %}
{% if postgresql_exists == 'false' %}
  initdb:  # List options to be passed on to initdb
    - encoding: {{ postgresql_encoding }}
    - locale: {{ postgresql_locale }}
  {% if postgresql_data_checksums|bool %}
    - data-checksums
  {% endif %}
{% endif %}

  pg_hba:  # Add following lines to pg_hba.conf after running 'initdb'
    - host replication {{ patroni_replication_username }} 127.0.0.1/32 md5
    - host all all 0.0.0.0/0 md5


postgresql:
{% if not with_haproxy_load_balancing|bool and not pgbouncer_install|bool and (cluster_vip is defined and cluster_vip | length > 0) %}
  listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}
{% else %}
  listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
{% endif %}
  connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ postgresql_port }}
  use_unix_socket: true
  data_dir: {{ postgresql_data_dir }}
  bin_dir: {{ postgresql_bin_dir }}
  config_dir: {{ postgresql_conf_dir }}
  pgpass: {{ postgresql_home_dir }}/.pgpass_patroni
  authentication:
    replication:
      username: {{ patroni_replication_username }}
      password: {{ patroni_replication_password }}
    superuser:
      username: {{ patroni_superuser_username }}
      password: {{ patroni_superuser_password }}
#    rewind:  # Has no effect on postgres 10 and lower
#      username: rewind_user
#      password: rewind_password
  parameters:
    unix_socket_directories: {{ postgresql_unix_socket_dir }}
{% if postgresql_stats_temp_directory_path is defined and postgresql_stats_temp_directory_path != 'none' %}
    stats_temp_directory: {{ postgresql_stats_temp_directory_path }}
{% endif %}

{% if postgresql_pg_ident is defined and postgresql_pg_ident | length > 0 %}
  pg_ident:
  {% for ident in postgresql_pg_ident %}
    - {{ ident.mapname }} {{ ident.system_username }} {{ ident.pg_username }}
  {% endfor %}
{% endif %}

  remove_data_directory_on_rewind_failure: {{ patroni_remove_data_directory_on_rewind_failure |string |d(false, true) |lower }}
  remove_data_directory_on_diverged_timelines: {{ patroni_remove_data_directory_on_diverged_timelines |string |d(false, true) |lower }}

#  callbacks:
#    on_start:
#    on_stop:
#    on_restart:
#    on_reload:
#    on_role_change:

  create_replica_methods:
{% if patroni_create_replica_methods is defined and patroni_create_replica_methods | length > 0 %}
  {% for create_replica_method in patroni_create_replica_methods %}
    - {{ create_replica_method }}
  {% endfor %}
  {% if 'pgbackrest' in patroni_create_replica_methods %}
  pgbackrest:
    {% for item in pgbackrest %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
  {% if 'wal_g' in patroni_create_replica_methods %}
  wal_g:
    {% for item in wal_g %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
  {% if 'basebackup' in patroni_create_replica_methods %}
  basebackup:
    {% for item in basebackup %}
    {{ item.option }}: '{{ item.value }}'
    {% endfor %}
    {% if postgresql_version is version('10', '>=') and postgresql_wal_dir is defined and postgresql_wal_dir | length > 0 %}
    waldir: {{ postgresql_wal_dir }}
    {% endif %}
  {% endif %}
  {% if 'pg_probackup' in patroni_create_replica_methods %}
  pg_probackup:
    {% for item in pg_probackup %}
    {{ item.option }}: {{ item.value }}
    {% endfor %}
  {% endif %}
{% else %}
    - basebackup
  basebackup:
    max-rate: '100M'
{% endif %}

{% if postgresql_restore_command is defined and postgresql_restore_command | length > 0 %}
  recovery_conf:
    restore_command: {{ postgresql_restore_command }}
{% endif %}

watchdog:
  mode: off  # Allowed values: off, automatic, required
  device: /dev/watchdog
  safety_margin: 5

tags:
  nofailover: false
  noloadbalance: false
  clonefrom: false
  nosync: false

  # specify a node to replicate from (cascading replication)
#  replicatefrom: (node name)

